package com.mzy.linear_struct.面试150题;

/**
 * @Author: mizy3
 * @Date: 2024/08/16/下午3:56
 * @Description:
 */
public class Pro82删除排序链表中的重复元素II {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(-1, head);
        ListNode ans = dummy;
        ListNode slow = dummy.next;
        ListNode fast = dummy.next.next;
        //   s
        // 1 2 2 2 3 3 4
        //         f

        //  d     s
        // -1 1,1,1,2,3
        //          f
        while (dummy != null && slow != null && fast != null) {
            if (slow.val == fast.val) {
                while (fast != null && slow.val == fast.val) {
                    fast = fast.next;
                }
                slow = fast;
                dummy.next = slow;
                if(fast != null){
                    fast = fast.next;
                }

            } else {
                dummy = dummy.next;
                fast = fast.next;
                slow = slow.next;
            }
        }

        return ans.next;
    }


}
